function [neighbours_sorted] = distance_sort(dest, neighbours, locations)

neighbours_sorted = [];
distances_sorted = [];
distance = @(source,loc)( sqrt(sum((loc - repmat( source,size(loc,1),1 ) ).^2,2) ) );

% initialize ajacency matrix
for i=1:length(neighbours)
    distances(i) = distance( dest, locations(neighbours(i)));
end

for i=1:length(neighbours)
    minDistance = 0;
    currentNode = 0;
    for j=1:length(neighbours)
        if distances(j) > minDistance & ~any(neighbours_sorted(:) == neighbours(j))
            minDistance = distances(j);
            currentNode = neighbours(j);
        end
    end
    if currentNode ~= 0
        distances_sorted = [distances_sorted minDistance];
        neighbours_sorted = [neighbours_sorted currentNode];
    end
end

neighbours_inv = neighbours_sorted;
neighbours_sorted = [];

for i=length(neighbours_inv):-1:1
    neighbours_sorted(end+1) = neighbours_inv(i);
end

end


